package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxDiagnosisListItem;
import com.novellotus.iov.carvice.bean.RxVehicle;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2018/1/10.
 */

public class DiagnosisListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterBottomInterface {

    private List<RxDiagnosisListItem.RecordsBean> recordsBeanList;
    private Context context;
    final int BOTTOM = 100;
    private boolean showBottom = false;
    private DiagnosisItemClickListener diagnosisItemClickListener;

    public DiagnosisListAdapter(List<RxDiagnosisListItem.RecordsBean> recordsBeanList, DiagnosisItemClickListener diagnosisItemClickListener) {
        this.recordsBeanList = recordsBeanList;
        this.diagnosisItemClickListener = diagnosisItemClickListener;
        if (this.recordsBeanList == null) {
            this.recordsBeanList = new ArrayList<>();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == recordsBeanList.size()) {
            return BOTTOM;
        } else {
            return 1;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new BottomHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_diagnosis_item, parent, false);
            return new DiagnosisHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == recordsBeanList.size()) {
            initBottom((BottomHolder) holder, position);
        } else {

            if (recordsBeanList.get(position) != null) {
                initCarHolder((DiagnosisHolder) holder, position);
            }

        }
    }

    @Override
    public int getItemCount() {
        if (showBottom) {
            return recordsBeanList.size() + 1;
        } else {
            return recordsBeanList.size();
        }
    }


    private void initBottom(BottomHolder holder, int position) {
        holder.tv_bottom.setTextColor(Color.parseColor("#333333"));
//        holder.tv_bottom.setAlpha(0.3f);
//        holder.view_left.setAlpha(0.3f);
//        holder.view_right.setAlpha(0.3f);
    }

    private void initCarHolder(DiagnosisHolder holder, final int position) {
        if (recordsBeanList.get(position) != null) {
            holder.tv_plate.setText(MyUtils.getPlateString(recordsBeanList.get(position).getPn()));
            holder.tv_vin.setText(recordsBeanList.get(position).getVin());
            holder.tv_model.setText(recordsBeanList.get(position).getName());
            holder.tv_statue.setVisibility(View.INVISIBLE);
            holder.tv_notice.setText(recordsBeanList.get(position).getCode());
            if (position == recordsBeanList.size() - 1) {
                holder.view_hide.setVisibility(View.VISIBLE);
            } else {
                holder.view_hide.setVisibility(View.GONE);
            }
            //是否报警
            switch (recordsBeanList.get(position).getAlarmStatus()) {
                case 1:
                    //充电
                    holder.tv_statue.setVisibility(View.VISIBLE);
                    holder.tv_statue.setBackgroundResource(R.drawable.shape_one_alram);
                    holder.tv_statue.setText("一级报警");
                    holder.view_line.setBackgroundColor(context.getResources().getColor(R.color.one_stage_background));
                    break;
                case 2:
                    //行驶
                    holder.tv_statue.setVisibility(View.VISIBLE);
                    holder.tv_statue.setBackgroundResource(R.drawable.shape_two_alarm);
                    holder.tv_statue.setText("二级报警");
                    holder.view_line.setBackgroundColor(context.getResources().getColor(R.color.two_stage_background));
                    break;
                case 3:
                    //未充电
                    holder.tv_statue.setVisibility(View.VISIBLE);
                    holder.tv_statue.setBackgroundResource(R.drawable.shape_corners_alarm);
                    holder.tv_statue.setText("三级报警");
                    holder.view_line.setBackgroundColor(context.getResources().getColor(R.color.three_stage_background));
                    break;
            }
            holder.rlt_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    diagnosisItemClickListener.clickDiagnosis(recordsBeanList.get(position).getPn(), recordsBeanList.get(position).getVin());
                }
            });
        }

    }


    public void updateList(List<RxDiagnosisListItem.RecordsBean> recordsBeanList) {
        this.recordsBeanList = recordsBeanList;
        showBottom = recordsBeanList.size() >= 4;
        notifyDataSetChanged();
    }

    public void addList(List<RxDiagnosisListItem.RecordsBean> recordsBeanList) {
        this.recordsBeanList.addAll(recordsBeanList);
        showBottom = recordsBeanList.size() >= 4;
        notifyDataSetChanged();
    }

    @Override
    public void showBottomLayout() {
        showBottom = recordsBeanList.size() >= 5;
        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }

    class DiagnosisHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_plate)
        TextView tv_plate;
        @BindView(R.id.tv_vin)
        TextView tv_vin;
        @BindView(R.id.tv_model)
        TextView tv_model;
        @BindView(R.id.tv_notice)
        TextView tv_notice;
        @BindView(R.id.tv_statue)
        TextView tv_statue;
        @BindView(R.id.view_hide)
        View view_hide;
        @BindView(R.id.view_line)
        View view_line;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;

        DiagnosisHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlt_bottom)
        RelativeLayout rlt_bottom;
        @BindView(R.id.tv_bottom)
        TextView tv_bottom;
        @BindView(R.id.view_left)
        View view_left;
        @BindView(R.id.view_right)
        View view_right;

        BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface DiagnosisItemClickListener {
        void clickDiagnosis(String plate, String vin);
    }
}
